<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" href="${ctxPath}/assets/module/formSelects/formSelects-v4.css"/>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card card-box-shadow">
        <div class="layui-card-body">
            <!-- 表格顶部工具栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">姓名：</label>
                        <div class="layui-input-inline mr0">
                            <input name="nickName" class="layui-input" type="text" placeholder="输入姓名"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="输入账号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="searchSubmit" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <% if(so.hasPermission("user:add")){ %>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <% } %>
                        <div class="dropdown-menu dropdown-hover">
                            <button class="layui-btn icon-btn">
                                <i class="layui-icon">&#xe681;</i>导入<i class="layui-icon layui-icon-drop"></i>
                            </button>
                            <ul class="dropdown-menu-nav dropdown-bottom-right">
                                <div class="dropdown-anchor"></div>
                                <li><a id="btnImport"><i class="layui-icon layui-icon-upload-drag"></i>批量导入</a>
                                </li>
                                <li>
                                    <a href="${ctxPath}/assets/file/用户导入模板.xls" target="_blank">
                                        <i class="layui-icon layui-icon-download-circle"></i>下载模板
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <% if(so.hasPermission("user:update")){ %>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <% } %>
    <% if(so.hasPermission("user:delete")){ %>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <% } %>
    <% if(so.hasPermission("user:modifyPsw")){ %>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
    <% } %>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableState">
    <input type="checkbox" lay-filter="ckState" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.state=='正常'?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelUser">
    <form id="modelUserForm" lay-filter="modelUserForm" class="layui-form model-form">
        <input name="id" id="id" type="hidden"/>
        <input name="unique" id="unique" type="hidden" class="layui-input"/>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md4">
                <label class="layui-form-label">公司</label>
                <div class="layui-input-block">
                    <input id="companyName" name="companyName" placeholder="请输入公司" type="text" class="layui-input" maxlength="20"
                           lay-verType="tips" lay-verify="required" readonly />
                    <input id="companyId" name="companyId" placeholder="" type="hidden" class="layui-input"
                           lay-verType="tips" lay-verify="required"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input name="name" id="name" placeholder="请输入账号" type="text" class="layui-input" maxlength="20"
                           autocomplete="off" lay-verType="tips" lay-verify="required|userNameUnique"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input name="nickName" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20" autocomplete="off"
                           lay-verType="tips" lay-verify="required"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md4">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="男" title="男" checked/>
                    <input type="radio" name="sex" value="女" title="女"/>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block">
                    <select name="roleIds" xm-select="roleIds" lay-verType="tips" lay-verify="required">
                        <% for(role in roles) { %>
                        <option value="${role.roleId}">${role.roleName}</option>
                        <% } %>
                    </select>
                </div>
            </div>
            <div class="layui-col-md4">
                <label class="layui-form-label">生日</label>
                <div class="layui-input-block">
                    <input  id="birthday" name="birthday"  type="text" class="layui-input date-icon"  autocomplete="off" lay-verType="tips" lay-verify="" placeholder="yyyy年MM月dd日"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelUserSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'formSelects', 'tableX', 'upload', 'dropdown', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var formSelects = layui.formSelects;
        var tableX = layui.tableX;
        var upload = layui.upload;
        var laydate = layui.laydate;

        // 渲染表格
        var insTb = tableX.render({
            elem: '#userTable',
            url: 'user/list',
            page: true,
            toolbar: true,
            cellMinWidth: "100",
            cols: [[
                {type: 'numbers'},
                {field: 'companyId', title: 'companyId', sort: true,hide:true},

                {field: 'companyName', minWidth: "200", title: '所属公司', sort: false},
                {field: 'name', title: '账号', sort: true},
                {field: 'nickName', title: '用户名', sort: true},
                {field: 'sex', title: '性别',minWidth: "30", sort: true},
                {field: 'birthday', minWidth: "160", title: '生日', sort: true, templet: function (d) {
                        return d.birthday;
                    }
                },
                {field: 'roleIds', title: 'roleIds', sort: true,hide:true},
                {field:'roleNames',minWidth: "160",
                    /*templet: function (d) {
                        var str = '';
                        for (var i = 0; i < d.roles.length; i++) {
                            str += ('<span class="layui-badge-rim">' + d.roles[i].roleName + '</span>');
                        }
                        return str;
                    },*/ title: '角色'
                },
                {
                    field: 'createTime',minWidth: "150", sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {field: 'state', templet: '#tableState', title: '状态', sort: true},
                {fixed: 'right',align: 'center', toolbar: '#tableBar', title: '操作',   minWidth: 150}
            ]]
        });

        // 添加
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(searchSubmit)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
        });

        // 工具条点击事件
        table.on('tool(userTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.nickName);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.id, data.nickName);
            }
        });

        // 显示表单弹窗
        function showEditModel(mUser) {
            admin.open({
                type: 1,
                maxmin:true,
                closeBtn:1,
                resize: true,
                area:["800px","500px"],
                title: (mUser ? '修改' : '添加') + '用户',
                content: $('#modelUser').html(),
                success: function (layero, dIndex) {
                    laydate.render({
                        elem: '#birthday'
//                        ,type: 'datetime'
                    });
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = mUser ? 'user/update' : 'user/add';
                    // 回显数据
                    var roleIds = new Array();
                    if (mUser) {
                        // $('#modelUserForm input[name="username"]').attr('readonly', 'readonly');
                        form.val('modelUserForm', mUser);
                        // for (var i = 0; i < mUser.roles.length; i++) {
                        //     roleIds.push(mUser.roles[i].roleId);
                        // }
                        roleIds = mUser.roleIds.split(",");
                    } else {
                        form.render('radio');
                    }
                    formSelects.render('roleIds', {init: roleIds});
                    formSelects.btns('roleIds', ['select', 'remove', 'reverse'], {space: '15px'});
                    // 表单提交事件
                    form.on('submit(modelUserSubmit)', function (data) {
                        data.field.roleIds = formSelects.value('roleIds', 'valStr');
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });

                    $("#companyName").click(function(){
                        var companyId = $("#companyId").val()==undefined?0:$("#companyId").val()
                        var layindex_t1 = admin.open({
                            type: 2,
                            content:"${ctxPath}/system/company/companySel?id="+companyId,
                            maxmin: true,
                            resize: true,
                            area: ["1000px", "540px"],
                            title: '请选择园区',
                            success: function (layero, dIndex) {

                            }
                        });
                    });
                    top.companySel = function(data_sel){
                        var t_data = data_sel[0];
                        $("#companyName").val(t_data.name);
                        $("#companyId").val(t_data.id);
                    }
                    form.verify({
                        userNameUnique: function(value) {
                            if($("#unique").val()!="canuse" && $("#unique").val()!=""){
                                return $("#unique").val();
                            }
                        }
                    });
                    $("#name").keyup(function(){
                        $("button[lay-filter=modelUserSubmit]").attr({"disabled":"disabled"});
                        $.ajax({
                            url: '../system/user/userNameUnique',// 这里写你要验证的地址哦。
                            type:"post",
                            data: { //要提交到服务端验证的用户名
                                id: $("#id").val(),
                                name: $("#name").val()
                            },
                            dataType: 'json',
                            success: function(result) { //栗子，返回的数据结果：{"rel":true,"message":"用户名已存在！"}
                                if (result.code==500) {
                                    $("#unique").val(result.msg);
                                } else {
                                    $("#unique").val("canuse");
                                }
                                $("button[lay-filter=modelUserSubmit]").removeAttr("disabled");
                            }
                        });

                    });
                }
            });
        }

        // 删除
        function doDel(ids, nickName) {
            layer.confirm('确定要删除“' + nickName + '”吗？', {
                shade: .1,
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.post('user/delete', {
                    ids: ids
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        // 修改用户状态
        form.on('switch(ckState)', function (obj) {
            layer.load(2);
            $.post('user/updateState', {
                id: obj.elem.value,
                state: obj.elem.checked ? "正常" : "锁定"
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        // 重置密码
        function resetPsw(id, nickName) {
            layer.confirm('确定要重置“' + nickName + '”的登录密码吗？', {
                shade: .1,
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.post('user/restPsw', {
                    id: id,
                    deft:true
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        // 导入
        upload.render({
            elem: '#btnImport',
            url: 'user/import',
            accept: 'file',
            exts: 'xls',
            before: function () {  // 上传前的回调
                layer.load(2);
            },
            done: function (res) {  // 上传完毕回调
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload();
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },
            error: function () {  // 请求异常回调
                layer.closeAll('loading');
                layer.msg('导入失败', {icon: 2});
            }
        });

    });
</script>

</body>
</html>